package com.tg.message.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tange.base.toolkit.DateUtil;
import com.tange.core.media.source.impl.local.DeviceStoragePlayback;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.message.R;

import java.util.List;

public class MessagePlaybackAdapter extends RecyclerView.Adapter<MessagePlaybackAdapter.PlaybackViewHolder> {

    private final List<DeviceStoragePlayback.Index> mData;
    private int currentPlayIndex = 0;

    private long startTime = 0;

    public void setStartTime(long start){
        if (startTime == 0){
            this.startTime = start;
        }
    }

    public void initStartTime(){
        this.startTime = 0;
    }

    public long getStartTime(){
        return startTime;
    }

    private OnItemClickListener onMessageItemListener;

    public void setOnMessageItemListener(OnItemClickListener onMessageItemListener) {
        this.onMessageItemListener = onMessageItemListener;
    }

    public void setCurrentPlayIndex(int currentPlayIndex) {
        this.currentPlayIndex = currentPlayIndex;
        notifyDataSetChanged();
    }

    public int getCurrentPlayIndex(){
        return currentPlayIndex;
    }

    public long getTimes(){
        if (mData != null && currentPlayIndex < mData.size() && currentPlayIndex > -1){
            return mData.get(currentPlayIndex).getEnd() - mData.get(currentPlayIndex).getStart();
        }

        return 0;

    }
    public MessagePlaybackAdapter(List<DeviceStoragePlayback.Index> list) {
        this.mData = list;
    }

    @NonNull
    @Override
    public PlaybackViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_playback_item, parent, false);
        return new PlaybackViewHolder(view);
    }


    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(@NonNull PlaybackViewHolder holder, int position) {
        DeviceStoragePlayback.Index bean = mData.get(position);

        holder.timetv.setText(DateUtil.getMinStrToTime(bean.getStart()));
        holder.msgtv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        holder.msgtv.setText(getEventName(bean.getEvent()));
        holder.msgtv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

        if (currentPlayIndex == position) {
            holder.root.setAlpha(1.0f);
            holder.timetv.setTextColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_black));
            holder.msgtv.setTextColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_black));
        } else {
            holder.root.setAlpha(0.4f);
            holder.timetv.setTextColor(ResourcesUtil.getResources().getColor(R.color.playback_no_select_color));
            holder.msgtv.setTextColor(ResourcesUtil.getResources().getColor(R.color.playback_no_select_color));
        }

        holder.itemView.setOnClickListener(v -> {
            if (onMessageItemListener != null) {
                onMessageItemListener.onItemClick(position);
            }
        });
    }

    public static String getEventName(int event){
        switch (event){
            case 1:
                return ResourcesUtil.getString(R.string.message_event_move);
            case 2:
                return ResourcesUtil.getString(R.string.message_event_body);
            case 3:
                return ResourcesUtil.getString(R.string.message_event_sound);
            case 4:
                return "PIR";
            case 5:
                return "烟雾";
            case 6:
                return ResourcesUtil.getString(R.string.txt_low_temp_tip);
            case 7:
                return ResourcesUtil.getString(R.string.txt_high_temp_tip);
            case 8:
                return ResourcesUtil.getString(R.string.txt_low_humid_tip);
            case 9:
                return ResourcesUtil.getString(R.string.txt_high_humid_tip);
            case 10:
                return ResourcesUtil.getString(R.string.event_gsensor);
            case 11:
            case 15:
                return ResourcesUtil.getString(R.string.txt_device_car);
            case 12:
                return ResourcesUtil.getString(R.string.message_filter_event_start);
            case 13:
                return ResourcesUtil.getString(R.string.device_car_parking_monitoring);
            case 14:
                return ResourcesUtil.getString(R.string.message_filter_event_speed_up);
            case 16:
                return ResourcesUtil.getString(R.string.lock_bell_someone_ring);
            case 17:
                return ResourcesUtil.getString(R.string.message_filter_event_doorbell_passby);
            case 18:
                return ResourcesUtil.getString(R.string.message_filter_event_stop);
            case 19:
                return ResourcesUtil.getString(R.string.device_function_lock_dell_msg);
        }
        return "";
    }
    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    static class PlaybackViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        TextView msgtv;
        TextView timetv;
        View root;

        public PlaybackViewHolder(@NonNull View convertView) {
            super(convertView);
            root = convertView.findViewById(R.id.root);
            iv = convertView.findViewById(R.id.iv);
            iv.setVisibility(View.GONE);
            msgtv = convertView.findViewById(R.id.msgtv);
            timetv = convertView.findViewById(R.id.timetv);
        }
    }
}
